//
//  JYTMySettingViewController.m
//  JYT_IOS
//
//  Created by nxa-chengjinhui on 2017/1/3.
//  Copyright © 2017年 nxa-yuanguoxiang. All rights reserved.
//

#import "JYTMySettingViewController.h"
#import "JYTTableViewCell.h"
#import "JYTMine.h"
#import "JYTChangePasswardViewController.h"
#import "JYTPostIdearViewController.h"
#import "JYTAboutJYTViewController.h"

@interface JYTMySettingViewController ()<UITableViewDelegate, UITableViewDataSource, UIAlertViewDelegate>
@property (nonatomic,strong)NSArray *dataArr;
@property (nonatomic,strong)UITableView *mytableView;

//@property (nonatomic,strong) UITableView *tabview;
//
//@property (nonatomic,strong) NSMutableArray *dataArray;

@end

@implementation JYTMySettingViewController

//-(NSMutableArray *)dataArray{
//    if (!_dataArray) {
//        _dataArray = [NSMutableArray arrayWithObjects:@"修改密码",@"关于驾易通",@"意见反馈",@"退出账号", nil];
//    }
//    return _dataArray;
//}
//

- (void)viewDidLoad {
    [super viewDidLoad];

//    self.view.backgroundColor = [UIColor whiteColor];
//    
//    NSDictionary *dic = @{@"lftBtnBgImg":@"ic_goback",
//                          @"lftBtnTitle":@"no",
//                          @"pageTitle":@"设 置",
//                          @"rhtBtnBgImg":@"no",
//                          @"rhtBtnTitle":@"no"};
//    
//    [self setNavgationBarWithDic:dic];
//
//    
//    [self initSubviews];

    self.view.backgroundColor = [UIColor whiteColor];
    
    NSDictionary *dic = @{@"lftBtnBgImg":@"ic_goback",
                          @"lftBtnTitle":@"no",
                          @"pageTitle":@"设 置",
                          @"rhtBtnBgImg":@"no",
                          @"rhtBtnTitle":@"no"};
    
    [self setNavgationBarWithDic:dic];
    
    
    [self initData];
    [self initView];
}


- (void)initData{
    
    
    NSArray *contentArr = [NSArray arrayWithObjects:@"修改密码",@"意见反馈",@"关于驾易通", @"退出账号", nil];
    
    
    NSMutableArray *temp = [NSMutableArray array];
    for (NSInteger i = 0; i < contentArr.count; i ++) {
        JYTMine *mine = [[JYTMine alloc]init];
        mine.content = contentArr[i];
        [temp addObject:mine];
    }
    
    NSArray *temp1 = [[NSArray alloc]initWithObjects:temp[0],temp[1],temp[2], nil];
    NSArray *temp2 = [[NSArray alloc]initWithObjects:temp[3], nil];
    
    
    self.dataArr = [[NSArray alloc]initWithObjects:temp1,temp2, nil];
}

//-(void)initSubviews{
//    
//    if (!self.tabview) {
//        self.tabview = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, CURRENT_SCREEN_WIDTH, CURRENT_SCREEN_HEIGHT-64) style:UITableViewStyleGrouped];
//        self.tabview.delegate = self;
//        self.tabview.dataSource = self;
//        self.tabview.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
//        self.tabview.backgroundColor = kColor(241, 241, 241);
//        [self.view addSubview:self.tabview];
//    }
//}
//
//#pragma mark --UITableViewDataSource UITableViewDelegate
//-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
////    return self.dataArray.count;
//    if (section==1) {
//        return 2;
//    }else{
//        return 1;
//    }
//}
//
//-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
//    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"celll"];
//    if (!cell) {
//        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"celll"];
//    }
//    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
//    if (indexPath.section==0) {
//        cell.textLabel.text = self.dataArray[0];
//    }else if (indexPath.section==1){
//        if (indexPath.row==0) {
//            cell.textLabel.text = self.dataArray[1];
//        }else{
//            cell.textLabel.text = self.dataArray[2];
//        }
//    }else{
//        cell.textLabel.text = self.dataArray[3];
//    }
//    
////    cell.backgroundColor = [UIColor clearColor];
//    return cell;
//}
//
//-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
//    return 3;
//}
//
//-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
//    return 60;
//}
//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
//    return 5;
//}
//-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
//    return 5;
//}
//
//-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
//    switch (indexPath.section) {
//        case 0:
//            [self pushViewController:[[JYTAlertPassViewController alloc] init]];
//            break;
//        case 1:
//        {
//            if (indexPath.row==0) {//关于
//                MyLog(@"guanyu ");
//            }else{//意见反馈
//                [self pushViewController:[[JYTAlertPassViewController alloc] init]];
//            }
//        }
//            break;
//        case 2:
//            MyLog(@"tuichu denglu ");
//            break;
//        default:
//            break;
//    }
//}

- (void)initView{
    self.mytableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, CURRENT_SCREEN_WIDTH, CURRENT_SCREEN_HEIGHT - 64) style:UITableViewStyleGrouped];
    self.mytableView.backgroundColor = [UIColor whiteColor];
    //[self.mytableView registerClass:[JYTTableViewCell class] forCellReuseIdentifier:@"mineCell"];
    self.mytableView.delegate = self;
    self.mytableView.dataSource = self;
    //self.mytableView.bounces = NO;
    
    [self.view addSubview:self.mytableView];
    
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 3;
    }else {
        return 1;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
   // JYTTableViewCell *cell = [self.mytableView dequeueReusableCellWithIdentifier:@"mineCell"];
   // cell.mine = [self.dataArr[indexPath.section] objectAtIndex:indexPath.row];
    
    static NSString *ID = @"cellset";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    if (indexPath.section == 0) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    } else {
        cell.textLabel.textAlignment = NSTextAlignmentCenter;
    }
    
    JYTMine *mine = [self.dataArr[indexPath.section] objectAtIndex:indexPath.row];
    [cell.textLabel setText:mine.content];
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return 44 *(CURRENT_SCREEN_HEIGHT / 667);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    
    return 5.0f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 5.0f;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row == 0) {
            JYTChangePasswardViewController *change = [[JYTChangePasswardViewController alloc]init];
           [self pushViewController:change];
        } else if(indexPath.row == 1){
            JYTPostIdearViewController *idear = [[JYTPostIdearViewController alloc]init];
            [self pushViewController:idear];
        } else {
            JYTAboutJYTViewController *about = [[JYTAboutJYTViewController alloc]init];
             [self pushViewController:about];
        }
        
        
        
    } else {
        //UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示：" message:@"您确定要退出当前用户" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
        //[alert show];
        
        
        
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:@"退出后不会删除任何历史数据,下次登录依然可以使用本账号." preferredStyle: UIAlertControllerStyleActionSheet];
        
        //@“ UIAlertControllerStyleAlert，改成这个就是中间弹出"
        
        UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        UIAlertAction *deleteAction = [UIAlertAction actionWithTitle:@"退出登录" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            [[JYTUserManager shared] removeSaveInfomation];
            NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
            [ud removeObjectForKey:@"user"];
            [ud removeObjectForKey:@"password"];
            [self goBack];
            
        }];
       
        [alertController addAction:cancelAction];
        [alertController addAction:deleteAction];
        //            [alertController addAction:archiveAction];
        [self presentViewController:alertController animated:YES completion:nil];

        
    }
    
}
#pragma mark----UIAlertViewDelegate
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex==0) {
        [alertView dismissWithClickedButtonIndex:buttonIndex animated:YES];
    }else{//跳转到登陆界面
        [[JYTUserManager shared] removeSaveInfomation];
        NSUserDefaults *ud = [NSUserDefaults standardUserDefaults];
        [ud removeObjectForKey:@"user"];
        [ud removeObjectForKey:@"password"];
        [self goBack];
    }
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
